package viewpagerindicator;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;

import java.util.List;

/**
 *
 */
public class ViewpagerAdapter extends PagerAdapter {
	private SparseArray<View> mListViews;
	private int mCount;
	
	public ViewpagerAdapter(List<View> lv)	{
		mCount = lv.size();
		mListViews = new SparseArray<View>(mCount);
		for(int i = 0 ; i <mCount;i++){
			View view = lv.get(i);
			bindView(view,i);
			mListViews.put(i, view);
		}		
	}
	
	public ViewpagerAdapter(Context context ,int[] list){
		mListViews = new SparseArray<View>();		
		mCount = list.length;
		for(int i = 0 ; i <mCount;i++){
			View view = LayoutInflater.from(context).inflate(list[i], null);
			bindView(view,i);
			mListViews.put(i, view);
		}
	}
	
	protected void bindView(View view, int location) {
		
	}

	@Override
	public int getCount() {
		return mCount;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return  arg0 == (arg1);
	}
	
	@Override
	public Object instantiateItem(View container, int position) {
        
		//position = position % (mCount);
	    View V;
		try{
		    
			((ViewPager)container).addView(mListViews.get(position), 0);
		
		}catch(Exception ex){
			
		}
		finally{
			V = mListViews.get(position);
		}
		return V;
	}	
	
	@Override
	public void destroyItem(View container, int position, Object object) {
		//position = position % (mCount);
		((ViewPager)container).removeView(mListViews.get(position));
	}


	@Override
	public void finishUpdate(View arg0) {
		
	}


	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		
	}


	@Override
	public Parcelable saveState() {
		return null;
	}


	@Override
	public void startUpdate(View arg0) {
		
	}		
	
	
	public void rebindView(){
		
		if(mListViews != null && mListViews.size() > 0){
			for(int i = 0 ; i <mCount;i++){
				bindView(mListViews.get(i),i);
			}						
		}	
	}
	
	public View getView(int index){
		
		if(mListViews != null){
			
			return mListViews.get(index);
		}
		
		return null;
		
	}

}